"use strict";
var Subject = function (text) {
    if(text){
        var obj = JSON.parse(text);
        this.id = obj.id;
        this.title = obj.title;
        this.belongAddress = obj.belongAddress;
        this.createtime = obj.createtime;
        this.itemCount = obj.itemCount;
        this.like = obj.like;
        this.status = obj.status;  // open /close
    }else{
        this.id = '';
        this.title = '';
        this.belongAddress = '';
        this.createtime = '';
        this.itemCount = 0;
        this.like = 0;
        this.status = '';
    }
};
Subject.prototype = {
    toString: function () {
        return JSON.stringify(this);
    }
};
var Item = function (text) {
    if(text){
        var obj = JSON.parse(text);
        this.id = obj.id;
        this.belongSubjectId = obj.belongSubjectId;
        this.belongAddress = obj.belongAddress;
        this.belongTitle = obj.belongTitle;
        this.content = obj.content;
        this.createtime = obj.createtime;
        this.like = obj.like;
        this.status = obj.status;  // open /close
    }else{
        this.id = '';
        this.belongSubjectId = '';
        this.belongAddress = '';
        this.belongTitle = '';
        this.content = '';
        this.createtime = '';
        this.like = 0;
        this.status = '';
    }
};
Item.prototype = {
    toString: function () {
        return JSON.stringify(this);
    }
};
var contractService = function (text) {
    //配置合约所有者
    LocalContractStorage.defineMapProperty(this, "config", null);
    // subjectID => subject
    LocalContractStorage.defineMapProperty(this, "subjectMap", {
        parse: function (text) {
            return JSON.parse(text);
        },
        stringify: function (o) {
            return JSON.stringify(o);
        }
    });
    //subjectSize
    LocalContractStorage.defineProperty(this,"subjectSize",null);
    //itemSize
    LocalContractStorage.defineProperty(this,"itemSize",null);


    //subjectID => array of item
    LocalContractStorage.defineMapProperty(this, "subjectItemMap", {
        parse: function (text) {
            return JSON.parse(text);
        },
        stringify: function (o) {
            return JSON.stringify(o);
        }
    });
    //address => array of subject
    LocalContractStorage.defineMapProperty(this, "addressSubjectMap", {
        parse: function (text) {
            return JSON.parse(text);
        },
        stringify: function (o) {
            return JSON.stringify(o);
        }
    });
    //address => array of Item
    LocalContractStorage.defineMapProperty(this, "addressItemMap", {
        parse: function (text) {
            return JSON.parse(text);
        },
        stringify: function (o) {
            return JSON.stringify(o);
        }
    });

};
contractService.prototype = {
    toString: function () {
        return JSON.stringify(this);
    },
    init: function () {
        this.config.set("owner",Blockchain.transaction.from);

    },
    getSubject:function () {
        var subjectSize = this.subjectSize || 0;
        var data = {};
        var subjectArray = [];
        for(var i = 1;i <= subjectSize;i++){
            subjectArray.push(this.subjectMap.get(i));
        }
        data.result = subjectArray;
        return data;
    },
    getItemOfSubject:function (subjectId) {
        var data = this.subjectItemMap.get(subjectId);
        return data;
    },
    getInfoOfAddress:function () {
        var fromAddress = Blockchain.transaction.from;
        var data = {};
        var items = this.addressItemMap.get(fromAddress);
        var subjects = this.addressSubjectMap.get(fromAddress);
        data.items = items;
        data.subjects = subjects;
        return data;
    },
    addSubject:function (title) {
        var fromAddress = Blockchain.transaction.from;
        var createtime = Blockchain.block.timestamp;
        if(title.length < 1 || title == ''){
            throw new Error("must set a title");
        }
        var newSubject = new Subject();
        var preGuessId = this.subjectSize || 0;
        newSubject.id = preGuessId + 1;
        newSubject.title = title;
        newSubject.belongAddress = fromAddress;
        newSubject.createtime = createtime;
        newSubject.itemCount = 0;
        newSubject.like = 0;
        newSubject.status = 'open';
        this.subjectMap.set(newSubject.id,newSubject);
        this.subjectSize = newSubject.id;
        var subjectArray = this.addressSubjectMap.get(fromAddress) || [];
        subjectArray.push(newSubject);
        this.addressSubjectMap.set(fromAddress,subjectArray);
        return this.getSubject();
    },
    addItem: function(belongSubjectId,belongTitle,content){
        var fromAddress = Blockchain.transaction.from;
        var createtime = Blockchain.block.timestamp;
        if(content.length < 1 || content == ''){
            throw new Error("must set a content");
        }
        var newItem = new Item();
        var preGuessId = this.itemSize || 0;
        newItem.id = preGuessId + 1;
        newItem.belongSubjectId = belongSubjectId;
        newItem.belongAddress = fromAddress;
        newItem.belongTitle = belongTitle;
        newItem.content = content;
        newItem.createtime = createtime;
        newItem.like = 0;
        newItem.status = 'open';
        this.itemSize = newItem.id;

        var subject = this.subjectMap.get(belongSubjectId);
        subject.itemCount++;

        this.subjectMap.set(belongSubjectId,subject);
        var subjectArray = this.addressSubjectMap.get(fromAddress) || [];
        for(var i = 0; i < subjectArray.length;i++){
            if(subjectArray[i].id == belongSubjectId)
                subjectArray[i].itemCount++;
        }
        this.addressSubjectMap.set(fromAddress,subjectArray);

        var itemArray = this.subjectItemMap.get(belongSubjectId) || [];
        itemArray.push(newItem);
        this.subjectItemMap.set(belongSubjectId,itemArray);
        var itemArray2 = this.addressItemMap.get(fromAddress) || [];
        itemArray2.push(newItem);
        this.addressItemMap.set(fromAddress,itemArray2);
        return this.getItemOfSubject(belongSubjectId);
    },
    likeSubject: function(subjectId){
        var subject = this.subjectMap.get(subjectId) || '';
        if(subject == '')
            throw new Error("subjectID incurrent");
        subject.like ++;
        this.subjectMap.set(subjectId,subject);
        return this.getSubject();
    },
    likeItem: function (itemId,subjectId) {
       var itemArray = this.subjectItemMap.get(subjectId);
       for(var i = 0; i < itemArray.length; i++){
           if(itemArray[i].id = itemId)
               itemArray[i].like++;
       }
       this.subjectItemMap.set(subjectId,itemArray);
       return this.getItemOfSubject(subjectId);
    }

};
module.exports = contractService;
